package com.demo2do.fms.admin.entity.report.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class OilPriceReportSearch implements QueryBuilder {

    /** 选择的公司 */
    private List<Long> organizationIds;
    
    /** 导出条数限制 */
    private Integer limit;
    
    private QueryFilter filter;
    
    private QueryOrder order;
    
    /**
     * The default constructor
     */
    public OilPriceReportSearch() {
        
        order = new QueryOrder().add("op", "organization_id", "ASC")
                                .add("op", "effect_date", "ASC");
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if (filter == null || refresh) {
            
            filter = new QueryFilter().addSentence("op.organization_id IN (:organizationIds)", "organizationIds", organizationIds);
            
        }
        
        return filter;
        
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.entity.report.ExportBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        StringBuffer buffer = new StringBuffer(256);
        
        buffer.append("select c.organization_name AS organizationName, ")
                     .append("(SELECT value FROM TBL_ALL_MASTER_DATA WHERE type IN ('OIL_OUTSIDE','OIL_INSIDE') AND code = OP.OIL_TYPE) AS oilType, ")
                     .append("(SELECT value FROM TBL_ALL_MASTER_DATA WHERE type = 'OIL_PLACE' and code = OP.OIL_PLACE) AS oilPlace, ")
                     .append("op.price AS price, ")
                     .append("op.start_date AS startDate, ")
                     .append("op.effect_date AS effectDate ")
             .append("FROM TBL_TRUCK_OIL_PRICE op, vw_transportation_company c ")
             .append("WHERE op.organization_id = c.id " + getPreparedFilter(false).clauseSentence() + order.sentence());
        
        String sentence = "SELECT * FROM (" + buffer.toString() + ") WHERE ROWNUM <=" + limit;
        return sentence;
        
    }
    
    /**
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {
        
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {
            
            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("organizationName", rs.getObject("organizationName"));
                result.put("oilType", rs.getObject("oilType"));
                result.put("oilPlace", rs.getObject("oilPlace"));
                result.put("price", rs.getObject("price"));
                result.put("startDate", rs.getObject("startDate"));
                result.put("effectDate", rs.getObject("effectDate"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }

    /**
     * @return
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the limit
     */
    public Integer getLimit() {
        return limit;
    }

    /**
     * @param organizationIds
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

}
